import { defaultTagRE } from "./parser";
// [{name: xxx< value: 'xxx'}] => {xxx: value,}
function genProps(attrs) {
  let str = "";
  for (let i = 0; i < attrs.length; i++) {
    let attr = attrs[i];
    if (attr.name === "style") {
      let styleObj = {};
      attr.value.replace(/([^;:]+)\:([^;:]+)/g, function () {
        styleObj[arguments[1].trim()] = arguments[2].trim();
      });
      attr.value = styleObj;
    }
    str += `${attr.name}: ${JSON.stringify(attr.value)},`;
  }
  return `{${str.slice(0, -1)}}`;
}

function gen(el) {
  if (el.type === 1) {
    return generate(el);
  } else {
    let text = el.text;
    // return `_v('${text}')`
    if (!defaultTagRE.test(text)) {
      return `_v('${text}')`;
    } else {
      // 'hello' + arr + 'world'
      let tokens = [];
      let match;
      let lastIndex = (defaultTagRE.lastIndex = 0);
      while ((match = defaultTagRE.exec(text))) {
        let index = match.index;
        if (index > lastIndex) {
          tokens.push(JSON.stringify(text.slice(lastIndex, index)));
        }
        // tokens.push(match[1].trim());
        tokens.push(`_s(${match[1].trim()})`);
        lastIndex = index + match[0].length;
      }
      if (lastIndex < text.length) {
        tokens.push(JSON.stringify(text.slice(lastIndex)));
      }

      return `_v(${tokens.join("+")})`;
    }
  }
}

function genChildren(el) {
  let children = el.children;
  if (children) {
    return children.map((c) => gen(c)).join(",");
  }
  return false;
}

export function generate(el) {
  let children = genChildren(el);
  // 遍历树  拼接成字符串
  let code = `_c('${el.tag}', ${
    el.attrs.length ? genProps(el.attrs) : "undefined"
  }${children ? `,${children}` : ""})`;

  return code;
}
